package com.snopy.service.impl;


import com.snopy.mapper.CategoryCustomMapper;
import com.snopy.mapper.CategoryMapper;
import com.snopy.pojo.Category;
import com.snopy.pojo.vo.CategoryVO;
import com.snopy.pojo.vo.NewItemsVO;
import com.snopy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author snoopy
 * @date 2020/9/20 0020 13:13
 * @email 77912204@qq.com
 * @remark
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryCustomMapper categoryCustomMapper;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryAllRootLevelCat(int type) {
        Example example = new Example(Category.class);

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", type);
        List<Category> categories = categoryMapper.selectByExample(example);
        return categories;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVO> getSubCatList(int rootCatId) {
        return categoryCustomMapper.getSubCatList(rootCatId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
        public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {

        Map<String, Object> map = new HashMap<>();
        map.put("rootCatId", rootCatId);

        return categoryCustomMapper.getSixNewItemsLazy(map);
    }


}
